//
//  LoginViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/8/8.
//  Copyright © 2018年 wjk. All rights reserved.
//

import UIKit
import SwiftyJSON
import Moya

class LoginViewController: BaseViewController {

    @IBOutlet weak var btnCode: UIButton!
    @IBOutlet weak var tfPass: UITextField!
    @IBOutlet weak var tfCode: UITextField!
    @IBOutlet weak var tfMobile: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "登录"
        
        
        
        tfPass.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
        tfMobile.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
        tfCode.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
        
//        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: R.image.nav_back(), style: .plain, target: self, action: #selector(self.openMessage(_:)))
        
        // Do any additional setup after loading the view.
    }
    
    @objc func openMessage(_ bar : UIBarButtonItem) {
        self.navigationController?.popViewController(animated: true)
    }
 
    deinit {
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        (UIApplication.shared.delegate as! AppDelegate).isplayData.removeAll()
        loadCap()
    }
    
    func loadCap() -> Void {
        
        NetWorkRequest(.cap()) { (res) -> (Void) in
            self.btnCode.setBackgroundImage((res["image"] as! UIImage), for: .normal)
        }
    }
    
    @IBAction func getCode(_ sender: UIButton) {
        
        loadCap()
//        sender.startTimer(title: "s")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
//        self.navigationController?.setNavigationBarHidden(false, animated: animated);
    }

    @IBAction func login(_ sender: UIButton) {
        self.view.endEditing(true);
        
        if tfCode.text!.isEmpty {
            showMessage(message: "请先输入验证码")
            return
        }
        
        showHUD()
        NetWorkRequest(.login(username: tfMobile.text!, password: tfPass.text!, captcha: tfCode.text!)) { (res)  -> (Void) in
            if res["code"] as! String == "000000" {
                let data = res["data"] as! [String:Any]
                let access_token = data["access_token"] as! String//真实的token
                UserDefaults.User.set(access_token, forKey: .authorization)
                UserDefaults.User.set(true, forKey: .isLogin)
                DispatchQueue.main.async {
                     NotificationCenter.default.post(name: NSNotification.Name.UserDidLogin, object: nil)
                }
//                self?.navigationController?.popViewController(animated: true)                
                
                
                
                NetWorkRequest(.daibiaoget()) { (res) -> (Void) in
                    if res["code"] as! String == "000000" {
                        let jd = jsonFormater(data: res["data"] as Any)
                        if jd["name"].stringValue.isEmpty == false {
                            (UIApplication.shared.delegate as! AppDelegate).user_name = jd["phone"].stringValue
                        }
                    }
                }
                
                
                (UIApplication.shared.delegate as! AppDelegate).user_name = self.tfMobile.text!
                
                UserDefaults.standard.set(self.tfMobile.text!, forKey: "user_name")
//                UserDefaults.User.set(self.tfMobile.text!, forKey: .user_name)
                UIApplication.shared.delegate?.window?!.rootViewController = R.storyboard.main().instantiateInitialViewController()
                
            }else {
                self.tfCode.text = ""
                self.loadCap()
            }
        }
    }
    
    @IBAction func reg(_ sender: UIButton) {
        let vc = R.storyboard.login.registerViewController()
        vc?.isReg = true;
        self.navigationController?.pushViewController(vc!, animated: true);
    }
    
    @IBAction func forget(_ sender: UIButton) {
        let vc = R.storyboard.login.registerViewController()
        vc?.isReg = false;
        self.navigationController?.pushViewController(vc!, animated: true);
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func playHouse(_ sender: UIButton) {
        
//        (UIApplication.shared.delegate as! AppDelegate).isplayhouse = true
        
        showHUD()
        
        NetWorkRequest(.fairPlay()) { (res) -> (Void) in
            dismissHUD()
            if res["code"] as! String == "000000" {
                (UIApplication.shared.delegate as! AppDelegate).isplayData = jsonFormater(data: res)["data"].arrayValue
                
                if (UIApplication.shared.delegate as! AppDelegate).isplayData.count <= 0 {
                    showMessage(message: "暂无专场")
                    return
                }
                
                let da = (UIApplication.shared.delegate as! AppDelegate).isplayData.sample!
                
                (UIApplication.shared.delegate as! AppDelegate).isBuy = da["direction"].intValue
                
                let vc = R.storyboard.home.guaListViewController()!.showType(t: da["trade_type"].intValue - 1)
                vc.fair_id = da["id"].stringValue
                vc.type_name = da["name"].stringValue
                vc.trade_type_text = da["trade_type_text"].stringValue
                vc.trade_date = da["trade_date"].stringValue
                vc.websocket = da["websocket_url"].stringValue
                self.navigationController?.pushViewController(vc, animated: true)
                
                
            }
        }
//        self.navigationController?.pushViewController(R.storyboard.home.buyViewController()!, animated: true)
    }
    
}
